#include "folderdatasource.h"
#include "../../../11-util/02-log_manager/logmanager.h"

static Logger * listLogger = LogManager::getMissionLogger("__list");

FolderDatasource::FolderDatasource() {
    models = new QVector<FolderModel *>();
    FolderModel * f = new FolderModel();
    f->fullName = "xxx";
    f->name = "111";
    models->append(f);
}


/**
 * QString直接返回,不要再转换为QVariant,会导致显示不出来.
 * 需要判断,role是DisplayRole才返回fullName,一股脑返回会显示不出来.
 * int role
 * @brief FolderDatasource::data
 * @param index
 * @param role
 * @return
 */
QVariant FolderDatasource::data(const QModelIndex &index, int role) const {
    // if (!index.isValid())
    //     return QVariant();
    int row = index.row();
    FolderModel * f = models->at(row);
    listLogger->debug("FolderDatasource","Data","",QString("get data row: %1 role: %2").arg(row).arg(role));
    // return f->fullName;
     if(role == Qt::DisplayRole)
        return f->fullName;
     if(role == Qt::DecorationRole) {
         return f->getIcon();
     }
     return QVariant();
    // QVariant variant = QVariant::fromValue(f);
    // return variant;
}

// QModelIndex FolderDatasource::index(int row, int column,
//                                     const QModelIndex &parent) const {
//     //parent无效,返回root的索引.
//     if (parent.isValid() && parent.column() != 0)
//         return QModelIndex();

//     //根据父索引获取父item
//     FolderModel *parentItem = getItem(parent);

//     //无父item,返回root索引.
//     if (!parentItem)
//         return QModelIndex();

//     FolderModel *childItem = models->at(row);
//     if (childItem)
//         return createIndex(row, column, childItem);
//     return QModelIndex();

// }
QModelIndex FolderDatasource::parent(const QModelIndex &index) const {
    return QModelIndex();
}
int FolderDatasource::rowCount(const QModelIndex &parent) const {
    int count = models->count();
    listLogger->debug("FolderDatasource","Data","",QString("row count is %1").arg(count));
    return models->count();
     // return 0;
}

int FolderDatasource::columnCount(const QModelIndex &parent) const {
    return 1;
}

 void FolderDatasource::test() {
                         
}


QModelIndex FolderDatasource::index(int row, int column, const QModelIndex &parent) const
{
    //parent无效,返回root的索引.
    if (parent.isValid() && parent.column() != 0)
        return QModelIndex();

    LogManager::debug(QString("create index at row %1 column %2\n").arg(row).arg(column));

    FolderModel *item = models->at(row);
    if (item)
        return createIndex(row, column, item);
    return QModelIndex();
}


/**
 * 从指定位置插入指定个数的数据.
 * 列表操作的QModelIndex
 * insertRows的数据哪里来????
 * @brief FolderDatasource::insertRows
 * @param position
 * @param rows
 * @param index
 * @return
 */
bool FolderDatasource::appendRows(QVector<FolderModel *>* newModels)  {
    int rows  = newModels->count();
    int position = models->count() - 1;
    LogManager::debug(QString("InsertRows at %1 count: %2\n").arg(position).arg(rows));
    beginInsertRows( QModelIndex(), position, position + rows - 1);
    for (int i = 0; i < rows; ++i) {
        FolderModel * m = newModels->at(i);
         models->append(m);
    }
    endInsertRows();
    return true;
}


/**
 * 根据索引得到对应的model.
 * index内部包含了model的引用.
 * @brief QProjectModel::getItem
 * @param index
 * @return
 */
FolderModel *FolderDatasource::getItem(const QModelIndex &index) const {
    // if (index.isValid()) {
        FolderModel *item = static_cast<FolderModel*>(index.internalPointer());
        // if (item)
            return item;
    // }
    // return rootItem;
}

QVariant FolderDatasource::headerData(int section,Qt::Orientation orientation,int role)const {
    return "headerData";
}
